<template>
  <div>
    <ts-header title="接单范围" back></ts-header>
    <div class="range">
      <p class="title">设置地址作为接单区域</p>
      <div class="cell-input">
        <div class="cell-label">地址</div>
        <div class="cell-middle" @click="chooseRange">
          <img class="cm-icon" src="../../assets/imgs/icon/coordinate.png"/>
          <span class="cm-value" v-if="address" v-text="address"></span>
          <span class="cm-tips" v-else>点击选择</span>
        </div>
        <img class="cell-arrow" src="../../assets/imgs/icon/right.png"/>
      </div>
      <div class="submit">
        <ts-button @click="addOrderRange" :disabled="!(!!address)" size="large" type="primary">
          完成
        </ts-button>
      </div>
    </div>
  </div>
</template>

<script>
  import {ADD_ORDER_RANGE, GET_RANGE_INFO} from '../../assets/js/api'
  import {bd09togcj02} from '../../utils/commonUtils'

  export default {
    name: 'range',
    data() {
      return {
        //地址全称
        address: '',
        //接单范围
        range: null,
        //经度
        soo_lon: '',
        //纬度
        soo_lat: ''
      }
    },
    async activated() {
      let json = await this.$http.post(GET_RANGE_INFO, null, {show: true});
      if (json.result === 1) {
        let {address, range} = json.data;
        this.address = address;
        this.range = range * 1000;
      }
      this.chooseBack();
    },
    methods: {
      chooseBack() {
        let {address, location} = this.$route.query;
        if (address && location) {
          this.$router.goBack();
          this.address = address;
          location = location.split(',');
          this.soo_lon = location[0];
          this.soo_lat = location[1];
        }
      },
      chooseRange() {
        if ($prod) {
          // 进行定位
          baidumap_location.getCurrentPosition((result) => {
            const {latitude, longitude} = result;
            const center = bd09togcj02(parseFloat(longitude), parseFloat(latitude)).join(',');
            this.$router.push({path: '/range/rangeMap', query: {center, range: this.range}});
          }, (error) => {
            this.$modal.alert({message: error});
          });
        } else {
          //百度坐标 117.239516,31.824906
          let center = bd09togcj02(117.242081, 31.868821).join(',');
          //测试
          this.$router.push({path: '/range/rangeMap', query: {center, range: this.range}});
        }
      },
      async addOrderRange() {
        let {soo_lon, soo_lat, address} = this.$data;
        if(soo_lon&&soo_lat){
          let json = await this.$http.post(ADD_ORDER_RANGE, {soo_lon, soo_lat, address});
          if (json.result === 1) {
            this.$modal.alert({message: '添加成功'});
          }
        } else {
          this.$modal.alert({message: '请选择具体位置或不要与上次添加的位置一样'});
        }
      }
    }
  }
</script>

<style lang="scss" scoped="">
  @import "../../assets/css/mixin";

  .range {
    .title {
      padding: .25rem .22rem;
      color: #2E2E2E;
    }
    .cell-input {
      font-size: .26rem;
      background-color: #fff;
      display: flex;
      align-items: center;
      height: 42px;
      padding: 0 .25rem;
      position: relative;
      @include borderScale('top');
      @include borderScale('bottom');
      .cell-label {
        width: .80rem;
      }
      .cell-middle {
        flex: 1;
        min-width: 0;
        .cm-icon {
          position: relative;
          top: 1px;
          vertical-align: middle;
          width: 10px;
        }
        .cm-value {
          vertical-align: middle;
          display: inline-block;
          margin-left: 3px;
          width: 5.65rem;
          @include wot();
        }
        .cm-tips {
          color: #909090;
          @extend .cm-value;
        }
      }
      .cell-arrow {
        width: 6px;
      }
    }
    .submit {
      padding: 1.20rem .35rem;
    }
  }
</style>
